package com.taotao.portal.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.taotao.common.HttpClientUtil;
import com.taotao.common.JsonUtils;
import com.taotao.common.TaotaoResult;
import com.taotao.pojo.TbItemDesc;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.portal.pojo.ItemInfo;
import com.taotao.portal.service.ItemService;
@Service
public class ItemServiceImpl implements ItemService {
	@Value("${REST_BASE_PATH}")
	private String REST_BASE_PATH;
	@Value("${ITEM_INFO}")
	private String ITEM_INFO;
	@Value("${ITEM_DESC}")
	private String ITEM_DESC;
	@Value("${ITEM_PARAM}")
	private String ITEM_PARAM;
	
	//商品基础信息
	@Override
	public ItemInfo getItemById(long itemId) {
		try {
			String json = HttpClientUtil.doGet(REST_BASE_PATH+ITEM_INFO+itemId);
			if(!StringUtils.isBlank(json)){
				TaotaoResult result = TaotaoResult.formatToPojo(json, ItemInfo.class);
				if(result.getStatus()==200){
					return (ItemInfo) result.getData();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	//商品描述
	@Override
	public String getItemDescById(long itemId) {
		try {
			String json = HttpClientUtil.doGet(REST_BASE_PATH+ITEM_DESC+itemId);
			if(!StringUtils.isBlank(json)){
				TaotaoResult result = TaotaoResult.formatToPojo(json, TbItemDesc.class);
				if(result.getStatus()==200){
					TbItemDesc itemDesc=(TbItemDesc) result.getData();
					return itemDesc.getItemDesc();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//商品规格参数
	@Override
	public String getItemParamById(long itemId) {
		
		try {
			String json = HttpClientUtil.doGet(REST_BASE_PATH+ITEM_PARAM+itemId);
			if(!StringUtils.isBlank(json)){
				TaotaoResult result=TaotaoResult.formatToPojo(json, TbItemParamItem.class);
				if(result.getStatus()==200){
					TbItemParamItem itemParamItem=(TbItemParamItem) result.getData();
					String itemParam=itemParamItem.getParamData();
					//生成html
					// 把规格参数json数据转换成java对象
					List<Map> jsonToList = JsonUtils.jsonToList(itemParam, Map.class);
					StringBuffer sb=new StringBuffer();
					sb.append("<table cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" border=\"0\" class=\"Ptable\">\n");
					sb.append("<tbody>\n");
					for(Map map:jsonToList){
						sb.append("<tr>\n");
						sb.append("<th class=\"tdTitle\" colspan=\"2\">"+map.get("group")+"</th>\n");
						sb.append("</tr>\n");
						List<Map> listParam=(List<Map>) map.get("params");
						for(Map param:listParam){
							sb.append("<tr>\n");
							sb.append("<td class=\"tdTitle\">"+param.get("k")+"</td>\n");
							sb.append("<td>"+param.get("v")+"</td>\n");
							sb.append("</tr>\n");

						}
					}
					sb.append("</tbody>\n");
					sb.append("</table>");
					return sb.toString();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "";
	}

}
